import { createRouter, createWebHistory } from 'vue-router'

import Exercises from './components/Exercises.vue'
import ExercisesQuestions from './components/ExercisesQuestions.vue'
import Simulation from "./components/Simulation.vue"
import Administrators from "./components/Administrators.vue"
import User from "./components/User.vue"
import Personal from "./components/Personal.vue"

import OptionPages from './pages/OperationPages.vue'
import Login from './pages/Login.vue'
import Pages404 from './pages/404.vue'

import DataCheck from "./dataGain/DataCheck";

export const router = createRouter({
    // 4. 内部提供了 history 模式的实现。为了简单起见，我们在这里使用 hash 模式。
    history: createWebHistory(),
    routes: [
        {
            path: '/',
            name: 'Login',
            component: Login
        },
        {
            path: '/option/:username',
            name: 'option',
            component: OptionPages,
            beforeEnter: (to, from, next) => {
                if (to.name == 'option' && !DataCheck.methods.GetIsAuthenticated()) {
                    next({name: 'Login'})
                } else {
                    next()
                }
              },
            children: [
                {
                    path: 'exercises',
                      component: Exercises
                },
                {
                    path: 'exercisesQuestions:firstIndex:secondIndex',
                      component: ExercisesQuestions
                },
                {
                    path: 'simulation',
                    component: Simulation
                },
                {
                    path: 'administrators',
                    component: Administrators
                },
                {
                    path: 'personal',
                    component: Personal
                },
                {
                    path: 'user',
                    component: User
                }
            ]
        },
        {
            path: '/:w+',
            name: 'w404',
            component: Pages404
        },
    ], 
});